﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScaffoldIt.Configuration;

namespace ScaffoldIt
{
    public partial class FormEditParameter : Form
    {
        public FormEditParameter()
        {
            InitializeComponent();
            
            comboBoxType.Items.Clear();
            comboBoxType.Items.AddRange(Parameter.ParamTypes);
        }

        public static bool EditParameter(Parameter parameter)
        {
            using (var frm = new FormEditParameter())
            {
                frm.Init(parameter);
                frm.textBoxCode.Focus();
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    frm.Read(parameter);
                    return true;
                }
            }

            return false;
        }

        private void Read(Parameter parameter)
        {
            parameter.Code = textBoxCode.Text;
            parameter.Title = textBoxTitle.Text;
            parameter.Value = textBoxValue.Text;
            parameter.Description = textBoxDescription.Text;
            parameter.IsGlobal = checkBoxGlobal.Checked;
            parameter.Type = (string)comboBoxType.SelectedItem;
            parameter.Choices = textBoxChoices.Text.Split('\r').Select(s => s.Trim()).Collapse(",");
        }

        private void Init(Parameter parameter)
        {
            textBoxCode.Text = parameter.Code;
            textBoxTitle.Text = parameter.Title;
            textBoxDescription.Text = parameter.Description;
            textBoxValue.Text = parameter.Value;
            comboBoxType.SelectedItem = parameter.Type;
            checkBoxGlobal.Checked = parameter.IsGlobal;
            if (parameter.Choices != null)
                textBoxChoices.Text = parameter.Choices.Split(',').Select(s => s.Trim()).Collapse("\r\n");
        }

        private void textBoxCode_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = !Parameter.ValidateCode(textBoxCode.Text);
            if (e.Cancel)
                errorProvider1.SetError(textBoxCode, "The code is required and must be used as a code variable. You should use only letters and digits.");
        }

        private void comboBoxType_Validating(object sender, CancelEventArgs e)
        {
            e.Cancel = comboBoxType.SelectedItem == null;
            if (e.Cancel)
                errorProvider1.SetError(comboBoxType, "You must select a type.");
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.ValidateChildren())
                DialogResult = DialogResult.OK;
        }

        private void comboBoxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((string)comboBoxType.SelectedItem == Parameter.PARAM_CHOICE)
            {
                textBoxChoices.Visible = true; 
                labelChoices.Visible = true;
            }
            else
            {
                labelChoices.Visible = false;
                textBoxChoices.Visible = false;
            }
        }
    }
}
